﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace BotvaBot.Configuration
{
    public class GenericConfigElementCollection<T> : ConfigurationElementCollection
        where T : CustomConfigElementBase, new()
    {
        public List<T> m_Items = new List<T>();
        public List<T> Items
        {
            get { return m_Items; }
        }

        public T this[int index]
        {
            get
            {
                return base.BaseGet(index) as T;
            }
            set
            {
                if (base.BaseGet(index) != null)
                    base.BaseRemoveAt(index);
                base.BaseAdd(index, value);
            }
        }

        protected override void BaseAdd(ConfigurationElement element)
        {
            base.BaseAdd(element);
            m_Items.Add(element as T);
        }


        protected override ConfigurationElement CreateNewElement()
        {
            return new T();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((T)element).ElementKey;
        }
    }
}
